package uit.mmt02.activity;

import uit.mmt02.R;
import uit.mmt02.String.XuLyChuoi;
import uit.mmt02.gmail.GmailControl;
import uit.mmt02.mailControl.HotmailControl;
import uit.mmt02.mailControl.MdeamonControl;
import uit.mmt02.mailControl.YahooControl;
import uit.mmt02.share.DB_EmailLoginManager_Adapter;
import uit.mmt02.share.EmailLoginManager;
import uit.mmt02.share.showToast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;

public class loginActivity extends Activity
{
	final static String GMAIL = "gmail";
	final static String YAHOO = "yahoo";
	final static String HOTMAIL = "hotmail";
	final static String KHAC = "khac";
	showToast st;
	EditText et_username , et_password;
	CheckBox cb_remember;
	String tag = "loginActivity";
	String username;
	String password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		st = new showToast(getBaseContext());
		et_username = (EditText)findViewById(R.id.et_UsernameLogin);
		et_password = (EditText)findViewById(R.id.et_PasswordLogin);
		cb_remember = (CheckBox)findViewById(R.id.CheckBox_remember);
	}
	
	public void btn_onClick(View v)
	{
		ImageButton btn = (ImageButton)v;
		switch (btn.getId()) 
		{
		case R.id.btn_login:
//			st.showmess("login");
			username = et_username.getText().toString().trim();
			password = et_password.getText().toString().trim();
			if(username.length() > 0 && password.length() > 0 )
			{
				if(username.indexOf("@")>-1) // neu email dung dinh dang , co@
				{
					// thuc hien kiem tra username co ton tai khong
					check_login_to_server();
				}else
				{
					AlertDialogshow("Lỗi Login...", "Định dạng Email không đúng !", "Oh yeah!");
				}
			}else
			{
//				st.showmess("Dien username, password");
				AlertDialogshow("Lỗi Login...", "Vui lòng nhập cả Email và Mật Khẩu", "Oh yeah!");
			}
			break;

		default:
			break;
		}
	}
	
	public void AlertDialogshow(String title, String mess, String button)
	{
		AlertDialog alert = new AlertDialog.Builder(loginActivity.this).create();
		alert.setTitle(title);
		alert.setMessage(mess);
		alert.setButton(button, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				return;
				
			}
		});
		alert.show();
		
	}
	
	public void check_login_to_server() //kiem tra ket noi toi server
	{
		try {
			XuLyChuoi str = new XuLyChuoi();
			String loai_email = str.kt_loai_email_login(username);
			if(loai_email.startsWith("gmail.com"))
			{
				new check_login_gmail().execute("gmail");
			}
			else if(loai_email.startsWith("yahoo.com"))
			{
				new check_login_yahoo().execute("yahoo mail");
			}
			else if(loai_email.startsWith("hotmail.com"))
			{
				new check_login_hotmail().execute("hot mail");
			}else
				new check_login_mdaemon().execute("internal");
			
		} catch (Exception e) {
			// TODO: handle exception
			Log.i(tag, "Loi check login " + e.toString());
		}
	}
	
	private class check_login_gmail extends AsyncTask<String, Boolean, Boolean> //tien trinh kiem tra se duoc chay ngam
	{
		ProgressDialog pd = new ProgressDialog(loginActivity.this);
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pd.setMessage("Kết Nối Tới Server Gmail...");
			pd.show();
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			GmailControl g = new GmailControl(username, password);
			if(g.check_connect()) // ket noi thanh cong ... save tai khoan vao csdl
			{
				if (cb_remember.isChecked())
				{
					// goi ham luu tai khoan
					DB_EmailLoginManager_Adapter mydb = new DB_EmailLoginManager_Adapter(loginActivity.this);
					mydb.open();
					EmailLoginManager email = new EmailLoginManager();
					email.setUsername(username);
					email.setPassword(password);
					email.setNewmail(g.getN_newmail()); // lay so  mail chua doc cho vao CSDL
					mydb.insert(email);
					mydb.close();
					Log.i(tag, "insert " + username);
				}
				return true;
			}
			else
				return false;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			pd.dismiss();
			if(result)
			{
				Intent i = new Intent(loginActivity.this, uit.mmt02.activity.ListMailActivity.class);
				startActivity(i);
			}
			else
			{
				AlertDialogshow("Loi ket noi", "Kiểm tra lại Username, password", "OK");
			}
			super.onPostExecute(result);
		}
		
	} //
	
	private class check_login_yahoo extends AsyncTask<String, Boolean, Boolean> //tien trinh kiem tra se duoc chay ngam
	{
		ProgressDialog pd = new ProgressDialog(loginActivity.this);
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pd.setMessage("Kết Nối Tới Server Yahoo");
			pd.show();
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			YahooControl g = new YahooControl(username, password);
			if(g.check_connect()) // ket noi thanh cong ... save tai khoan vao csdl
			{
				if (cb_remember.isChecked())
				{
					// goi ham luu tai khoan
					DB_EmailLoginManager_Adapter mydb = new DB_EmailLoginManager_Adapter(loginActivity.this);
					mydb.open();
					EmailLoginManager email = new EmailLoginManager();
					email.setUsername(username);
					email.setPassword(password);
					email.setNewmail(g.getN_newmail()); // lay so  mail chua doc cho vao CSDL
					mydb.insert(email);
					mydb.close();
					Log.i(tag, "insert " + username);
				}
				return true;
			}
			else
				return false;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			pd.dismiss();
			if(result)
			{
				Intent i = new Intent(loginActivity.this, uit.mmt02.activity.ListMailActivity.class);
				startActivity(i);
			}
			else
			{
				AlertDialogshow("Loi ket noi", "Kiểm tra lại Username, password", "OK");
			}
			super.onPostExecute(result);
		}
		
	}//
	
	private class check_login_hotmail extends AsyncTask<String, Boolean, Boolean> //tien trinh kiem tra se duoc chay ngam
	{
		ProgressDialog pd = new ProgressDialog(loginActivity.this);
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pd.setMessage("Kết Nối Tới Server HotMail....");
			pd.show();
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			HotmailControl g = new HotmailControl(username, password);
			if(g.check_connect()) // ket noi thanh cong ... save tai khoan vao csdl
			{
				if (cb_remember.isChecked())
				{
					// goi ham luu tai khoan
					DB_EmailLoginManager_Adapter mydb = new DB_EmailLoginManager_Adapter(loginActivity.this);
					mydb.open();
					EmailLoginManager email = new EmailLoginManager();
					email.setUsername(username);
					email.setPassword(password);
					email.setNewmail(g.getN_newmail()); // lay so  mail chua doc cho vao CSDL
					mydb.insert(email);
					mydb.close();
					Log.i(tag, "insert " + username);
				}
				return true;
			}
			else
				return false;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			pd.dismiss();
			if(result)
			{
				Intent i = new Intent(loginActivity.this, uit.mmt02.activity.ListMailActivity.class);
				startActivity(i);
			}
			else
			{
				AlertDialogshow("Loi ket noi", "Kiểm tra lại Username, password", "OK");
			}
			super.onPostExecute(result);
		}
		
	}//
	
	private class check_login_mdaemon extends AsyncTask<String, Boolean, Boolean> //tien trinh kiem tra se duoc chay ngam
	{
		ProgressDialog pd = new ProgressDialog(loginActivity.this);
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pd.setMessage("Kết Nối Tới Server Noi bo...");
			pd.show();
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			MdeamonControl g = new MdeamonControl(username, password);
			if(g.check_connect()) // ket noi thanh cong ... save tai khoan vao csdl
			{
				if (cb_remember.isChecked())
				{
					// goi ham luu tai khoan
					DB_EmailLoginManager_Adapter mydb = new DB_EmailLoginManager_Adapter(loginActivity.this);
					mydb.open();
					EmailLoginManager email = new EmailLoginManager();
					email.setUsername(username);
					email.setPassword(password);
					email.setNewmail(g.getN_newmail()); // lay so  mail chua doc cho vao CSDL
					mydb.insert(email);
					mydb.close();
					Log.i(tag, "insert " + username);
				}
				return true;
			}
			else
				return false;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			pd.dismiss();
			if(result)
			{
				Intent i = new Intent(loginActivity.this, uit.mmt02.activity.ListMailActivity.class);
				startActivity(i);
			}
			else
			{
				AlertDialogshow("Loi ket noi", "Kiểm tra lại Username, password", "OK");
			}
			super.onPostExecute(result);
		}
		
	}//
}
